<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>

    <style>


    </style>
</head>

<body>
<h1>用户信息</h1>
<div>
    <p>用户名: <span id="username"></span></p>
    <p>登录后的 accessToken: <span id="accessToken"></span></p>
</div>
<hr>

</body>
</html>

<script type="text/javascript" src="/static/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">


    $(document).ready(function () {

        $("#accessToken").text(localStorage.getItem("accessToken"));


        //请求用户信息
        function requestUserInfo(){
            return new Promise((resolve, reject)=>{
                var accessToken = localStorage.getItem("accessToken");
                if(accessToken == "" || accessToken == undefined) {
                    reject("请先登录")
                    return
                }

                $.ajax({
                    type: 'GET', // 请求的方式
                    headers: {"Authorization": "Bearer " + accessToken,"Request-Type": "ajax"},
                    url: '/index/userInfo',  // 请求的 URL 地址
                    success: function (res, textStatus, xhr) { // 请求成功之后的回调函数

                        if(res.code != 200){
                            reject(res.message)
                            return;
                        }
                        resolve(res);
                    }
                });

            })
        }


        requestUserInfo().then(function (res){
            $("#username").text(res.data.username);

        }).catch(function (msg){
            alert(msg);
        })


    })


    /**
     * 获取指定的URL参数值
     * URL:http://www.xxx.com/index?name=123
     * 参数：param URL参数
     * 调用方法:getParam("name")
     * 返回值:123
     * alert(getParam('date'));
     */
    function getParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        //search,查询？后面的参数，并匹配正则
        var r = location.search.substr(1).match(reg);
        if (r != null) return decodeURI(decodeURI(r[2]));
    }


    /**
     * 删除url上指定参数，并发起请求
     * window.location.replace(urlDelParams('params'))
     * @param name
     * @return {string}
     */
    function urlDelParams(name) {
        var loca = window.location
        var baseUrl = loca.origin + loca.pathname + '?'
        var query = loca.search.substr(1)
        if (query.indexOf(name) > -1) {
            var obj = {}
            var arr = query.split('&')
            for (var i = 0; i < arr.length; i++) {
                arr[i] = arr[i].split('=')
                obj[arr[i][0]] = arr[i][1]
            }
            delete obj[name]
            var url =
                baseUrl +
                JSON.stringify(obj)
                    .replace(/[\"\{\}]/g, '')
                    .replace(/\:/g, '=')
                    .replace(/\,/g, '&')
            return url
        }
    }

</script>